export interface ConnectParams {
  host: string;
  port: string;
  username?: string;
  password?: string;
  tableName?: string;
}
/**
 请求的范型 传入 res.data的类型
 */
export type RequestBack<T> = Promise<{ data?: T; err?: Response }>;

export type uuid = string;

export interface DBS {
  databases: Array<{
    name: string;
    sizeOnDisk: number;
    empty: boolean;
  }>;
  totalSize: number; //数据库大小
  ok: number;
}

export interface DbInfo {
  id: uuid;
  dbs: DBS;
  databaseName: string;
}

export type DataBaseType = "mongo";

export interface Collection {
  name: string;
  options: Partial<{
    validationAction: string;
    validationLevel: string;
    validator: {
      $jsonSchema: {
        bsonType: string;
        properties: {
          [field: string]: { bsonType: string; description: string };
        }[];
        required: string[];
      };
    };
  }>;
  type: "collection";
  idIndex: {
    key: { _id: number };
    name: string;
  };
  info: {
    readOnly: boolean;
    uuid: uuid;
  };
}

export type Collections = Array<Collection>;

/**
 * @description: mongodb 常用的BSON类型
 */
export const BSONType = {
  string: "string",
  int: "int",
  double: "double",
  array: "array",
  bool: "bool",
  long: "long",
  object: "object",
  null: "null",
  timestamp: "timestamp",
  date: "date",
  objectId: "objectId",
  regex: "regex",
};
// TODO array 类型处
/**
 * @description: 把string转成对应的BSON数据传入后端
 * @param {any} value
 * @return {*} BSON
 */
export const transformBSON = {
  [BSONType.string]: (val: string) => String(val),
  [BSONType.int]: (val: string) => {
    console.log(val, typeof val);
    return Number(val);
  },
  [BSONType.bool]: (val: string) => val === "true",
  [BSONType.double]: (val: string) => Number(val),
  [BSONType.null]: (val: string) => null,
  [BSONType.objectId]: (val: string) => ({
    // 命名 zxczvsv_special  防止对象中恰巧存在同名属性
    zxczvsv_special: true, //zxczvsv_special 特殊类型让后端处理
    type: BSONType.objectId, //告诉后端什么类型
    val, //需要处理的值
  }),
  [BSONType.timestamp]: (val: string) => ({
    zxczvsv_special: true, //zxczvsv_special 特殊类型让后端处理
    type: BSONType.timestamp, //告诉后端什么类型
    val, //需要处理的值
  }),
  [BSONType.date]: (val: string) => ({
    zxczvsv_special: true, //zxczvsv_special 特殊类型让后端处理
    type: BSONType.date, //告诉后端什么类型
    val, //需要处理的值
  }),
  [BSONType.regex]: (val: string) => val,
};
